<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\Process;
use common\models\Department;

class SyncProcess extends Model
{
	public $pcode;

	public function formName()
    {
        return '';
    }

	public function rules()
	{
		return [
			['pcode', 'required'],
		];
	}

	public function getProcess()
	{
		$process = Process::find()->where(['pcode' => $this->pcode])->orderBy('status')->all();
		if ($process) {
			$re_arr = [];
			$i = 0;
			foreach ($process as $key => $val) {
					if ($val->order == 1) {
						$re_arr[$i]['id'] = $val->tran_depart;
						$re_arr[$i]['pid'] = 0;
						$re_arr[$i]['name'] = Department::findOne($val->tran_depart)->name;
						$re_arr[$i + 1]['id'] = $val->undertake_depart;
						$re_arr[$i + 1]['pid'] = $val->tran_depart;
						$re_arr[$i + 1]['name'] = Department::findOne($val->undertake_depart)->name;
						$i = $i + 2;
					} else {
						$re_arr[$i]['id'] = $val->undertake_depart;
						$re_arr[$i]['pid'] = $val->tran_depart;
						$re_arr[$i]['name'] = Department::findOne($val->undertake_depart)->name;
						$i = $i + 1;
					}
				}

				$result = $this->returnArray($re_arr);

				if ($result) {

					return [
						'flag' => true,
						'process' => $result
					];
				}
		}

		return [
			'flag' => false
		];
	}

	/**
    * 将数组格式array('id'=> , 'pid'=> , 'name'=> )生成树形数组格式
    * array('id'=> , 'pid'=> , 'name'=> , childrens => array() )
    */
    function returnArray($result){
        
        $newResult = array();
        if( !empty($result) ){

            foreach ($result as $k => $v) {

                $arrTep = $v;
                $arrTep['childrens'] = array();

                //父类ID是0时，代表没有父类ID，为根节点
                if( 0 == $v['pid'] ){
                    $newResult[] = $arrTep;
                    continue;
                }

                if( 0 != $v['pid']){
                    //添加不入数组中的子节点，可能是没有父类节点，那么将其当成根节点
                    if(false === $this->updateArray($newResult, $arrTep) ){
                        $arrTep = array('id'=> $arrTep['id'], 'pid'=>0, 'name'=>$arrTep['name'], 'childrens'=>array($arrTep));
                        $newResult[] = $arrTep;
                    }

                }
            }
        }
        //测试数组是否生成树形数组
        //return $newResult;
        return $newResult;
    }

    /**
    * 将当前节点插入到新的树形数组中
    * @param $newResult 树形数组地址
    * @param $arrTep 当前节点
    */
    function updateArray( &$newResult, $arrTep ){

        if( !empty($newResult) ){
            foreach ($newResult as $k => $v) {
                //查询当前节点的id是否与新的树形数组的id一致，如果是，那么将当前节点存放在树形数组的childrens字段中
                if( $v['id'] == $arrTep['pid']){

                    $newResult[$k]['childrens'][] = $arrTep;
                    return true;

                }elseif( !empty($v['childrens']) ){
                    //递归调用，查询树形数组的子节点与当前节点的关系
                    if(true === $this->updateArray( $newResult[$k]['childrens'], $arrTep )){
                        return true;
                    }

                }

            }
        }
        return false;
    }

}